# -*- coding: utf-8 -*-
import scrapy
import json
from ..items import Jingdong1Item


class ShoubiaoSpider(scrapy.Spider):
    name = 'shoubiao'
    start_urls = [
        'https://search.jd.com/Search?keyword=手表&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手表&page=1&s=0',
        'https://search.jd.com/s_new.php?keyword=手表&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手表&page=2&s=28',
        'https://search.jd.com/Search?keyword=手表&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手表&page=3&s=56',
        'https://search.jd.com/s_new.php?keyword=手表&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手表&page=4&s=84',
        'https://search.jd.com/Search?keyword=手表&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手表&page=5&s=112',
        'https://search.jd.com/s_new.php?keyword=手表&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=手表&page=2&s=140']

    def parse(self, response):
        all_pinpai = response.xpath("string(//ul[@class='J_valueList v-fixed'])")[0].extract()
        all_pinpai = all_pinpai.replace('\t', '').replace('\n', '')[:46]
        all_leixing = response.xpath("//div[@class='sl-v-list']/ul[@class='J_valueList']/li/a/text()")[:4].extract()
        all_leixing = ','.join(all_leixing)
        all_li = response.xpath("//li[@class='gl-item']")
        for li in all_li:
            title = ','.join(
                li.xpath(".//div[@class='gl-i-wrap']/div[@class='p-name p-name-type-2']/a/em/text()").extract())
            jiage = li.xpath(".//div[@class='p-price']/strong/i/text()")[0].extract()
            image = 'https:' + li.xpath(".//div[@class='p-img']/a/img/@source-data-lazy-img")[0].extract()
            spid = li.xpath("./@data-sku")[0].extract()
            url_pl = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds=' + str(spid)
            yield scrapy.Request(url=url_pl, callback=self.parse_pl,
                                 meta={'title': title, 'jiage': jiage, 'image': image, 'spid': spid})

    def parse_pl(self, response):
        title = response.meta['title']
        jiage = response.meta['jiage']
        image = response.meta['image']
        spid = response.meta['spid']
        plnum = json.loads(response.text)['CommentsCount'][0]['CommentCountStr']
        url_xq = 'https://sclub.jd.com/comment/productPageComments.action?&productId=' + str(
            spid) + '&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
        yield scrapy.Request(url=url_xq, callback=self.parse_xq,
                             meta={'title': title, 'jiage': jiage, 'image': image, 'spid': spid, 'plnum': plnum})

    def parse_xq(self, response):
        title = response.meta['title']
        jiage = response.meta['jiage']
        image = response.meta['image']
        spid = response.meta['spid']
        plnum = response.meta['plnum']
        comments = json.loads(response.text)['comments']
        plcontent = []
        for cc in comments:
            pl = dict(nickname=cc['nickname'],
                      content=cc['content'],
                      creationTime=cc['creationTime'],
                      score=cc['score'])
            plcontent.append(pl)
        item = Jingdong1Item()
        item['title'] = title
        item['jiage'] = jiage
        item['image'] = image
        item['spid'] = spid
        item['plnum'] = plnum
        item['plcontent'] = plcontent
        print(item)
        yield item
